package com.edu.study.base.activationCode.service.impl;

import com.edu.base.common.utils.IdGen;
import com.edu.base.ums.umsMember.domain.UmsMember;
import com.edu.study.base.activationCode.domain.TbActivationCodeRy;
import com.edu.study.base.activationCode.mapper.TbActivationCodeRyMapper;
import com.edu.study.base.activationCode.service.ITbActivationCodeRyService;
import com.edu.study.base.schemaHandle.IEduMasterService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 激活码Service业务层处理
 * 
 * @author jjt
 * @date 2021-03-01
 */
@Service
//@DataSource(value= DataSourceType.SLAVE)
public class TbActivationCodeRyServiceImpl implements ITbActivationCodeRyService 
{
    @Autowired
    private TbActivationCodeRyMapper tbActivationCodeRyMapper;
    
	@Autowired
	private  IEduMasterService  eduMasterService;

    /**
     * 查询激活码
     * 
     * @param id 激活码ID
     * @return 激活码
     */
    @Override
    public TbActivationCodeRy selectTbActivationCodeRyById(String id)
    {
        return tbActivationCodeRyMapper.selectTbActivationCodeRyById(id);
    }

    /**
     * 查询激活码列表
     * 
     * @param tbActivationCodeRy 激活码
     * @return 激活码
     */
    @Override
    public List<TbActivationCodeRy> selectTbActivationCodeRyList(TbActivationCodeRy tbActivationCodeRy)
    {
        return tbActivationCodeRyMapper.selectTbActivationCodeRyList(tbActivationCodeRy);
    }

    /**
     * 新增激活码
     * 
     * @param tbActivationCodeRy 激活码
     * @return 结果
     */
    @Override
    public int insertTbActivationCodeRy(TbActivationCodeRy tbActivationCodeRy)
    {
        return tbActivationCodeRyMapper.insertTbActivationCodeRy(tbActivationCodeRy);
    }


    /**
     * 新增激活码
     *
     * @param tbActivationCodeRy 激活码
     * @return 结果
     */
    @Override
    public List<TbActivationCodeRy> batchInsert(TbActivationCodeRy tbActivationCodeRy)
    {

        String uuid = UUIDUtils.getUUID();
        Integer genCount = tbActivationCodeRy.getGenCount();
        List<TbActivationCodeRy> arrayList = new ArrayList<>();
        if(genCount!=null && genCount > 0){
            for (int i = 0; i < genCount; i++) {
                TbActivationCodeRy activationCode2 = new TbActivationCodeRy();
                activationCode2.setId(IdGen.uuid());
//                activationCode2.setIsNewRecord(true);
                activationCode2.setCatalogType(tbActivationCodeRy.getCatalogType());
                activationCode2.setBatchNo(uuid);
                activationCode2.setMaterialIfUse("2");
                String sqlno=genSeqNo(10,tbActivationCodeRy.getCatalogType());
                activationCode2.setMaterialSequenceNum(sqlno);
                String qrcodeUrl="http://lew.gt-english.com/static/app/app.html?code="+sqlno;
                activationCode2.setQrCodeUrl(qrcodeUrl);
                activationCode2.setEffectiveTermCycleType(tbActivationCodeRy.getEffectiveTermCycleType());
                activationCode2.setEffectiveTermValue(tbActivationCodeRy.getEffectiveTermValue());
                activationCode2.setGenerateType(tbActivationCodeRy.getGenerateType());
                if(StringUtils.isNotEmpty(tbActivationCodeRy.getApplicationScopeType())) {
                    activationCode2.setApplicationScopeType(tbActivationCodeRy.getApplicationScopeType());
                }

                arrayList.add(activationCode2);
            }
            tbActivationCodeRyMapper.batchInsert(arrayList);
        }
        return arrayList;
    }

    /**
     *激活码样式：LEW开头，10位数字+1个字母（数字和字母不要出现0和O），输入的激活码的时候不分字母大小写，但是后台生成的时候字母需要大写</div><div>后台生成激活码的时候需要激活码以及链接
     * @param length 需要的数字部分的长度
     * @return 返回随机字母数字组合字符串
     */
    public static String genSeqNo(int length,String catalogType) {
        String start = "";
        if (catalogType.equals("1")) {
            start = "MZHB";
        }else if (catalogType.equals("2")){
            start = "MZSP";
        }
        String val = "";
        Random random = new Random();
        int index=0;
        while(index<length) {
            int RandomNum = random.nextInt(10);
            while(RandomNum==0) {
                RandomNum=random.nextInt(10);
            }
            val=val+RandomNum;
            index++;
        }
        return start+val;
    }
    /**
     * 修改激活码
     * 
     * @param tbActivationCodeRy 激活码
     * @return 结果
     */
    @Override
    public int updateTbActivationCodeRy(TbActivationCodeRy tbActivationCodeRy)
    {
        return tbActivationCodeRyMapper.updateTbActivationCodeRy(tbActivationCodeRy);
    }

    /**
     * 批量删除激活码
     * 
     * @param ids 需要删除的激活码ID
     * @return 结果
     */
    @Override
    public int deleteTbActivationCodeRyByIds(String[] ids)
    {
        return tbActivationCodeRyMapper.deleteTbActivationCodeRyByIds(ids);
    }

    /**
     * 删除激活码信息
     * 
     * @param id 激活码ID
     * @return 结果
     */
    @Override
    public int deleteTbActivationCodeRyById(String id)
    {
        return tbActivationCodeRyMapper.deleteTbActivationCodeRyById(id);
    }

 	@SuppressWarnings("unlikely-arg-type")
	@Override
    public List<TbActivationCodeRy> selectTbActivationCodeRyListWithOther(TbActivationCodeRy tbActivationCodeRy) {
    	
    	List<TbActivationCodeRy> list = tbActivationCodeRyMapper.selectTbActivationCodeRyListWithOther(tbActivationCodeRy);
    	list.forEach(v-> {
    		if(org.apache.commons.lang3.StringUtils.isNotBlank(v.getAppUserId())) {
    			List<UmsMember> umlist = eduMasterService.getUmsMemberRy();
    			List<UmsMember> umsMemberList = umlist.stream().filter(x-> x.getId().equals(v.getAppUserId())).collect(Collectors.toList());
    			if(CollectionUtils.isNotEmpty(umsMemberList)) { 
    				v.setPhone(umsMemberList.get(0).getPhone());
        			v.setNickName(umsMemberList.get(0).getNickName());
    			} 
    		}
    	});
		return list;
     
    }
}
